#!/bin/sh

slowlog_path=/root/slow_query_log
everyslow=/tmp/everydayslow
##须要下载pt-query-digest
pt_digest=/root/pt-query-digest

start_string=$(grep $(date --date="0 days ago" +%y%m%d) ${slowlog_path} | head -1)
start_pos=$(grep -n $(date --date="0 days ago" +%y%m%d) ${slowlog_path} | head -1 | awk -F: '{print $1}')
end_pos=$(grep -n $(date --date="0 days ago" +%y%m%d) ${slowlog_path} | tail -1 | awk -F: '{print $1}')
print_linecnt=$(expr ${end_pos} - ${start_pos})

##start_string假设不为空，则分析日志
if [ -z "${start_string}" ]; then
    exit 1
else
    grep -i "${start_string}" -A ${print_linecnt} ${slowlog_path} >${everyslow}
    /usr/bin/perl ${pt_digest} ${everyslow} >/tmp/anaslowlog_$(date +%Y-%m-%d)
fi
